home *** CD-ROM | disk | FTP | other *** search
/ Freelog 125 / Freelog_MarsAvril2015_No125.iso / Musique / Quod Libet / quodlibet-3.3.0-installer.exe / bin / quodlibet / ext / events / rbimport.pyc (.txt) < prev    next >
Python Compiled Bytecode  |  2014-12-31  |  5KB  |  135 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.7)
  3.  
  4. import xml.sax as xml
  5. from xml.sax.handler import ContentHandler
  6. from gi.repository import Gtk
  7. from quodlibet import app
  8. from quodlibet import util
  9. from quodlibet.qltk.msg import WarningMessage, ErrorMessage
  10. from quodlibet.util.uri import URI
  11. from quodlibet.util.path import expanduser, normalize_path
  12. from quodlibet.plugins.events import EventPlugin
  13.  
  14. class RBDBContentHandler(ContentHandler):
  15.     
  16.     def __init__(self, library):
  17.         ContentHandler.__init__(self)
  18.         self._library = library
  19.         self._current = None
  20.         self._tag = None
  21.         self._changed_songs = []
  22.  
  23.     
  24.     def characters(self, content):
  25.         if self._current is not None and self._tag is not None:
  26.             self._current[self._tag] = content
  27.  
  28.     
  29.     def startElement(self, name, attrs):
  30.         self._tag = None
  31.         if name == 'entry' and attrs.get('type') == 'song':
  32.             self._current = { }
  33.         elif name in ('location', 'rating', 'play-count', 'last-played'):
  34.             self._tag = name
  35.  
  36.     
  37.     def endElement(self, name):
  38.         self._tag = None
  39.         if name == 'entry' and self._current is not None:
  40.             current = self._current
  41.             self._current = None
  42.             if len(current) > 1:
  43.                 uri = current.pop('location', '')
  44.                 
  45.                 try:
  46.                     p_uri = URI(uri)
  47.                 except ValueError:
  48.                     return None
  49.  
  50.                 if not p_uri.is_filename:
  51.                     return None
  52.                 None._process_song(normalize_path(p_uri.filename), current)
  53.             
  54.  
  55.     
  56.     def _process_song(self, path, stats):
  57.         song = self._library.get(path, None)
  58.         if not song:
  59.             return None
  60.         has_changed = None
  61.         if 'rating' in stats:
  62.             
  63.             try:
  64.                 value = int(stats['rating']) / 5
  65.             except ValueError:
  66.                 pass
  67.  
  68.             song['~#rating'] = value
  69.             has_changed = True
  70.         if 'play-count' in stats:
  71.             
  72.             try:
  73.                 value = int(stats['play-count'])
  74.             except ValueError:
  75.                 pass
  76.  
  77.             song['~#playcount'] = value
  78.             has_changed = True
  79.         if 'last-played' in stats:
  80.             
  81.             try:
  82.                 value = int(stats['last-played'])
  83.             except ValueError:
  84.                 pass
  85.  
  86.             if value > song('~#lastplayed', 0):
  87.                 song['~#lastplayed'] = value
  88.                 has_changed = True
  89.             
  90.         if has_changed:
  91.             self._changed_songs.append(song)
  92.  
  93.     
  94.     def finish(self):
  95.         '''Call at the end, also returns amount of imported songs'''
  96.         count = len(self._changed_songs)
  97.         self._library.changed(self._changed_songs)
  98.         self._changed_songs = []
  99.         return count
  100.  
  101.  
  102.  
  103. def do_import(parent, library):
  104.     db_path = expanduser('~/.local/share/rhythmbox/rhythmdb.xml')
  105.     handler = RBDBContentHandler(library)
  106.     
  107.     try:
  108.         xml.sax.parse(db_path, handler)
  109.     except Exception:
  110.         util.print_exc()
  111.         handler.finish()
  112.         msg = _('Import Failed')
  113.         ErrorMessage(parent, RBImport.PLUGIN_NAME, msg).run()
  114.  
  115.     count = handler.finish()
  116.     msg = _('Successfully imported ratings and statistics for %d songs') % count
  117.     WarningMessage(parent, RBImport.PLUGIN_NAME, msg).run()
  118.  
  119.  
  120. class RBImport(EventPlugin):
  121.     PLUGIN_ID = 'rbimport'
  122.     PLUGIN_NAME = _('Rhythmbox Import')
  123.     PLUGIN_DESC = _('Import ratings and song statistics from Rhythmbox')
  124.     
  125.     def PluginPreferences(self, *args):
  126.         button = Gtk.Button(label = _('Start Import'))
  127.         
  128.         def clicked_cb(button):
  129.             do_import(button, app.library)
  130.  
  131.         button.connect('clicked', clicked_cb)
  132.         return button
  133.  
  134.  
  135.